package cn.huijielong.modules.pub.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;

import cn.huijielong.common.utils.PageUtils;
import cn.huijielong.common.utils.Query;
import cn.huijielong.common.utils.RD;
import cn.huijielong.modules.cm.entity.ZoneEntity;
import cn.huijielong.modules.cm.service.ZoneService;
import cn.huijielong.modules.pub.entity.RegisterEntity;
import cn.huijielong.modules.pub.service.RegisterService;
import cn.huijielong.modules.sys.controller.AbstractController;




/**
 * 用户注册商户使用申请
 * 
 * @author xietong
 * @email xietong@gmail.com
 * @date 2019-08-10 15:26:59
 */
@RestController
@RequestMapping("/pub/register")
public class RegisterController  extends AbstractController{
	@Autowired
	private RegisterService registerService;
	@Autowired
	private ZoneService zoneService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public RD list(@RequestParam Map<String, Object> params){
	    logger.info("从{}发送请求，参数：{}",getHeader("referer"),params.toString());
		//查询列表数据
        Query query = Query.createRequest(params);
		Page<RegisterEntity> registerList = registerService.queryList(query);		
		PageUtils pageUtil = new PageUtils(registerList, registerList.getTotal(), query.getLimit(), query.getPage());
		return RD.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info")
	public RD info(){
		Long userId=getUserId();
		RegisterEntity register = registerService.queryObject(userId);
		RD rd=RD.ok();
		if(register!=null) {
			if(register.getProvinceCode()!=null) {
				List<ZoneEntity> cityList=zoneService.queryCities(register.getProvinceCode());
				String[] nameList=new String[cityList.size()];
				int index=-1;
				for(int i=0;i<cityList.size();i++) {
					ZoneEntity zone=cityList.get(i);
					nameList[i]=zone.getName();
					if(zone.getCode().equals(register.getCityCode())) {
						index=i;
					}
				}
				rd.put("cityList", cityList).put("cityNameList", nameList).put("cityIndex", index);
			}
			if(register.getCityCode()!=null) {
				List<ZoneEntity> countyList=zoneService.queryCounties(register.getCityCode());
				String[] nameList=new String[countyList.size()];
				int index=-1;
				for(int i=0;i<countyList.size();i++) {
					ZoneEntity zone=countyList.get(i);
					nameList[i]=zone.getName();
					if(zone.getCode().equals(register.getCountyCode())) {
						index=i;
					}
				}
				rd.put("countyList", countyList).put("countyNameList", nameList).put("countyIndex", index);
			}
			rd.put("register", register);
		}
		return rd;
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public RD save(@RequestBody RegisterEntity register){
		Long userId=getUserId();
		register.setUserId(userId);
		registerService.save(register);
		return RD.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public RD update(@RequestBody RegisterEntity register){
		registerService.update(register);
		return RD.ok();
	}
	
}
